﻿#region using

using System.Collections.Generic;
using System.IO;
using Microsoft.Office.Interop.Access;

#endregion

namespace MsBuildAccess.Common
{
    /// <summary>
    /// Access object class
    /// </summary>
    public class AccessObject
    {       
        #region properties

        /// <summary>
        /// Last error message
        /// </summary>
        public string ErrorMessage { get; private set; }

        /// <summary>
        /// Has error in last operation
        /// </summary>
        public bool HasError { get; private set; }

        /// <summary>
        /// Access database file name
        /// </summary>
        public string FileName { get; private set; }

        /// <summary>
        /// Acces object name
        /// </summary>
        public string ObjectName
        {
            get { return this.objectName; }
        }

        /// <summary>
        /// file extension
        /// </summary>
        public string FileExt
        {
            get { return this.fileExt; }
        }

        /// <summary>
        /// access object type
        /// </summary>
        public AcObjectType ObjectType { get; private set; }

        /// <summary>
        /// Only warning when has error message
        /// </summary>
        public bool HasWarning
        {
            get { return !string.IsNullOrEmpty(this.ErrorMessage); }
        }

        #endregion

        #region fields

        /// <summary>
        /// Dictonary for access object types
        /// </summary>
        private static readonly Dictionary<string, AcObjectType> dict = new Dictionary<string, AcObjectType>();

        /// <summary>
        /// file extension
        /// </summary>
        private readonly string fileExt;

        /// <summary>
        /// Current object name
        /// </summary>
        private readonly string objectName;

        #endregion

        #region constructor

        /// <summary>
        /// Initialize static member of {Dictionary} 
        /// </summary>
        static AccessObject()
        {
            lock (dict)
            {
                dict.Add("acq", AcObjectType.acQuery);
                dict.Add("acf", AcObjectType.acForm);
                dict.Add("acm", AcObjectType.acModule);
                dict.Add("acs", AcObjectType.acMacro);
                dict.Add("acr", AcObjectType.acReport);
                ////we don't need because exclude:acb,acn
                dict.Add("acb", AcObjectType.acTable);
                ////not really default
                dict.Add("acn", AcObjectType.acDefault);

                ////
                ////todo all
                /*dict.Add("",AcObjectType.acDefault);
                dict.Add("",AcObjectType.acDiagram);
                dict.Add("",AcObjectType.acDataAccessPage);
                dict.Add("",AcObjectType.acFunction);
                dict.Add("",AcObjectType.acServerView);
                dict.Add("",AcObjectType.acStoredProcedure);*/
            }
        }

        /// <summary>
        /// Initializes a new instance of AccessObject
        /// </summary>
        /// <param name="fileName">microsot access source controlled file name</param>
        public AccessObject(string fileName)
        {
            this.FileName = fileName;
            this.objectName = Path.GetFileNameWithoutExtension(fileName);
            this.fileExt = Path.GetExtension(fileName).ToLower().Trim().Substring(1);
            this.GetAccessObjetTypeByFileName();
            this.ErrorMessage = string.Empty;
        }

        #endregion

        /// <summary>
        /// Get access object type from filename
        /// </summary>               
        private void GetAccessObjetTypeByFileName()
        {
            if (dict.ContainsKey(this.FileExt.ToLower()))
            {
                this.ObjectType = dict[this.FileExt.ToLower()];
            }
            else
            {
                this.ErrorMessage = string.Format("Invalid file type: {0}", this.FileExt);
                this.HasError = true;
            }
        }
    }
}